//
//  BottomActionSheet.swift
//  GreenDeal
//
//  Created by Moennig Q on 14/12/2022.
//

import UIKit
import RxSwift
import RxCocoa
import SnapKit
class BottomActionSheet: UIView {
    fileprivate lazy var disposeBag = DisposeBag()
    var selectItemClosure: ((Int)->Void)?
    let tableView = UITableView()
    var dataObserver: BehaviorRelay<[String]> = BehaviorRelay(value: [])
    var selectIndex: Int?
    //
    var tableViewHeightConstrait: Constraint?
    //IB init
    required init?(coder aDecoder: NSCoder) {
        super.init(coder:aDecoder)
    }
    //纯代码创建
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setUpView()
    }
    private func setUpView() {
        self.backgroundColor = UIColor(hexStr: "#00000080")
        tableView.separatorStyle = .none
        tableView.backgroundColor = .white
        tableView.isScrollEnabled = false
        tableView.drawCorner(byRoundingCorners: [.topLeft,.topRight])
        self.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            self.tableViewHeightConstrait = make.height.equalTo(0).constraint
        }
        //上方空白区域，点击时关闭view
        let blankView = UIView()
        blankView.backgroundColor = .clear
        self.addSubview(blankView)
        blankView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.greaterThanOrEqualTo(0)
            make.bottom.equalTo(tableView.snp.top)
        }
        //点击空白区域关闭此pop view
        blankView.rx.tapGesture().when(.recognized).subscribe(onNext: { [weak self] _ in
            self?.removeFromSuperview()
        }).disposed(by: self.disposeBag)
        
        tableView.contentInset = UIEdgeInsets(top: 20, left: 0, bottom: 40, right: 0)
        tableView.register(BottomActionSheetCell.self, forCellReuseIdentifier: BottomActionSheetCell.reuseCellIdentifier)
        //设置cell信息
        dataObserver.bind(to: tableView.rx.items(cellIdentifier: BottomActionSheetCell.reuseCellIdentifier,cellType: BottomActionSheetCell.self)){[weak self] (row, text, cell) in
            cell.itemNameLabel.text = text
            if let selectIndex = self?.selectIndex, row == selectIndex {
                cell.itemNameLabel.textColor = UIColor(hexStr: "#379657")
                cell.itemNameLabel.backgroundColor = UIColor(hexStr: "#3796571A")
            }
        }.disposed(by: self.disposeBag)
        //选择某一样
        tableView.rx.itemSelected.bind{ [weak self](indexPath) in
            if let cell = self?.tableView.cellForRow(at: indexPath) as? BottomActionSheetCell{
                cell.itemNameLabel.textColor = UIColor(hexStr: "#379657")
                cell.itemNameLabel.backgroundColor = UIColor(hexStr: "#3796571A")
                self?.selectItemClosure?(indexPath.row)
                self?.removeFromSuperview()
            }
        }
        .disposed(by: disposeBag)
        //不选择
        tableView.rx.itemDeselected.bind{ [weak self](indexPath) in
            if let cell = self?.tableView.cellForRow(at: indexPath) as? BottomActionSheetCell{
                cell.itemNameLabel.textColor = UIColor(hexStr: Constant.ActionSheetTextColorHexStr)
                cell.itemNameLabel.backgroundColor = .white
            }
        }
        .disposed(by: disposeBag)
    }
    func loadData(){
        //初始化数据
        tableView.reloadData()
        self.tableViewHeightConstrait?.update(offset: BottomActionSheetCell.cellHeight * dataObserver.value.count +  40)
    }
}
        
class BottomActionSheetCell: UITableViewCell{
    static let cellHeight: Int = 50
    let itemNameLabel = ContentLabel(R.font.interMedium(size: 18))
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setUpviews()
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
    }
    func setUpviews(){
        self.contentView.backgroundColor = .white
        itemNameLabel.backgroundColor = .white
        itemNameLabel.textAlignment = .center
        itemNameLabel.drawCorner(byRoundingCorners: .allCorners, radii: 6)
        self.contentView.addSubview(itemNameLabel)
        itemNameLabel.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.height.equalTo(BottomActionSheetCell.cellHeight)
            make.bottom.equalToSuperview()
        }
    }
}
